package com.xssq.mvc.impl;

import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.MapUtils;

import com.xssq.mvc.ControllerResovler;
import com.xssq.mvc.Handler;
import com.xssq.mvc.HandlerMapping;
import com.xssq.mvc.Requester;

public class DefaultHandlerMapping implements HandlerMapping {

	/**
	 * 根据requestMethod和requestPath对应Handler
	 */
	@Override
	public Handler getHandler(String requestMethod, String requestPath) {
		Map<Requester, Handler> handlerMapping = ControllerResovler.getHandlerMapping();
		if (!MapUtils.isEmpty(handlerMapping)) {
			Set<Requester> requesters = handlerMapping.keySet();
			for (Requester requester : requesters) {
				String method = requester.getRequestMethod();
				String path = requester.getRequestPath();
				// 只是简单的比较path，暂时不支持参数绑定
				if (method.equals(requestMethod.toUpperCase()) && path.equals(requestPath)) {
					return handlerMapping.get(requester);
				}
			}
		}
		return null;
	}

}
